package com.project.service;

import com.project.dao.InternInfoMapper;
import com.project.entity.InternInfo;
import com.project.myBeans.InternInfoDetail;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by hasee on 2017/4/28.
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class InternInfoServiceImpl extends BaseService implements InternInfoService {
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private OrgInfoService orgInfoService;
    @Autowired
    private ImagesService imagesService;

    private static InternInfoMapper internInfoMapper = null;

    public void setMapper() {
        if (internInfoMapper == null)
            internInfoMapper = sqlSessionTemplate.getMapper(InternInfoMapper.class);//1.获取相关mapper
    }

    @Override
    public InternInfo findById(int id) {
        setMapper();
        return internInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int modify(InternInfo internInfo) {
        setMapper();
        return internInfoMapper.updateByPrimaryKeySelective(internInfo);
    }

    @Override
    public int add(InternInfo internInfo) {
        setMapper();
        return internInfoMapper.insertSelective(internInfo);
    }

    @Override
    public int delete(int id) {
        setMapper();
        return internInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<InternInfo> findall() {
        setMapper();
        return internInfoMapper.selectAll();
    }

    @Override
    public List<InternInfo> findByCondition(InternInfo internInfo) {
        setMapper();
        return internInfoMapper.selectByCondition(internInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 根据实习信息id查找实习详细信息
     *
     * @param id 实习信息id
     * @return
     */
    @Override
    public InternInfoDetail findDetailById(Integer id) throws Exception {
        setMapper();
        InternInfoDetail internInfoDetail = new InternInfoDetail();
        InternInfo internInfo = findById(id);
        internInfoDetail.setInternInfo(internInfo);
        if (internInfo.getSno() != null)
            internInfoDetail.setStudentInfoDetail(studentInfoService.findDetailById(internInfo.getSno()));
        if (internInfo.getOrgid() != null)
            internInfoDetail.setOrgInfo(orgInfoService.findByid(internInfo.getOrgid()));
        if (internInfo.getWorkingpic() != null)
            internInfoDetail.setImage(imagesService.findById(internInfo.getWorkingpic()));
        return internInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/7/27
     * function: 根据实习生id(sno)和实习单位类别查找正在实习的实习信息
     *
     * @param internInfo
     * @return
     */
    @Override
    public InternInfo findOneBySnoAndOragattr(InternInfo internInfo) {
        setMapper();
        return internInfoMapper.selectOneBySnoAndOragattr(internInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/8/3
     * function: 查找审核中的实习信息
     *
     * @param internInfo
     * @return
     */
    @Override
    public InternInfo findOneBySnoAndReview(InternInfo internInfo) {
        setMapper();
        return internInfoMapper.selectOneBySnoAndReview(internInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 判断是否处于审核流程中
     *
     * @param internInfo
     * @return ture - 不处于 false - 处于
     */
    @Override
    public boolean checkIsUnderReview(InternInfo internInfo) {
        boolean flag = false;
        if (internInfoMapper.selectOneBySnoAndReview(internInfo) != null)
            flag = true;

        return flag;
    }

    /**
     * author: WheelChen
     * date: 2017/9/19
     * function: 根据实习生idList批量删除
     *
     * @param snoList
     * @return
     */
    @Override
    public int removeBatchBySnoList(List snoList) {
        setMapper();
        return internInfoMapper.deleteBatchBySnoList(snoList);
    }
}
